const mysql = require('../db/mysql57');
const minio = require('../db/minio');

const printDao = {
    async createOrder(uid, type, address, name, phone, count, file, fileName, remark) {
        let connection, fileId, uri, ret;
        let browser = 'hcyj-printfile';
        // 开启事务
        try {
            connection = await mysql.startTransaction();
        } catch (error) {
            throw error;
        }
        // 插入文件记录
        try {
            let sql = `INSERT INTO PrintFile(uid, browser, uri, fileName, status) VALUES (?, ?, ?, ?, ?);`;
            let data = [ uid, browser, '', fileName, '待上传' ];
            let result = await mysql.sqlExecInTransaction(connection, sql, data);
            fileId = result.insertId;
            uri = `${fileId}.${file.fileType}`;
        } catch (err) {
            await mysql.rollbackTransaction(connection);
            throw err;
        }
        // 更新文件记录
        try {
            let sql = `UPDATE PrintFile SET uri = ?, status = ? WHERE id = ?`;
            let data = [ uri, '已完成', fileId ];
            await mysql.sqlExecInTransaction(connection, sql, data);
        } catch (err) {
            await mysql.rollbackTransaction(connection);
            throw err;
        }
        // 插入数据
        try {
            let sql = `insert into PrintOrder(uid, type, address, name, phone, count, fileId, remark) values (?, ?, ?, ?, ?, ?, ?, ?);`;
            let data = [ uid, type, address, name, phone, count, fileId, remark ];
            ret = await mysql.sqlExecInTransaction(connection, sql, data);
        } catch (err) {
            await mysql.rollbackTransaction(connection);
            throw err;
        }
        // 将文件上传到 minio 里
        try {
            await new Promise((resolve, reject) => {
                minio.minio.putObject(browser, uri, file.buffer, file.buffer.length, async function (err, etag) {
                    if (err) {
                        reject(err);
                        return ;
                    } else {
                        resolve(true);
                    }
                });
            });
        } catch (err) {
            await mysql.rollbackTransaction(connection);
            throw err;
        }
        // 提交事务
        try {
            mysql.commitTransaction(connection);
        } catch (err) {
            await mysql.rollbackTransaction(connection);
            throw err;
        }
        return ret;
    },
    getSelfOrderList(uid) {
        let sql = `select
                po.id as id,
                po.type as type,
                po.address as address,
                po.name as name,
                po.phone as phone,
                po.count as count,
                po.remark as remark,
                po.status as status,
                DATE_FORMAT(po.startOrderTime, '%Y-%m-%d %H:%i:%s') as startOrderTime,
                DATE_FORMAT(po.endOrderTime, '%Y-%m-%d %H:%i:%s') as endOrderTime,
                pf.fileName as fileName,
                pf.uri as fileUrl
            from PrintOrder as po
            left join PrintFile as pf on po.fileId = pf.id
            where po.uid = ?`;
        let data = [ uid ];
        return mysql.sqlExec(sql, data);
    }
};

module.exports = printDao;